import myReq from '../../request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsInfo: {},
    items: [
      // {value: 'friends', name: '朋友代付'},
      { value: 'self', name: '微信支付' }
    ],
    //选择地址
    // tag: {
    //   addrTag: false,
    //   carTag: false,
    //   payTag: false,
    //   shadowTag: true,
    // },
    animationData: {},
    shadowAnimation: {},
    //生成订单
    token: "",
    address_id: '',
    coupon_money: '',
    money: '',
    shopping_car_ids: [],
    //去支付
    delivery_time: '',
    timeStamp: '',
    nonceStr: '',
    package: '',
    signType: '',
    paySign: '',
    // 确认支付
    payStatus: false,
  },

  // 去支付
  goPay: function (obj) {
    let that = this;
    wx.showLoading({
      title: '加载中',
    })
    console.log("—1—token————>", that.data.token);
    console.log("—2—订单id————>", JSON.stringify(that.data.goodsInfo.order_id));

    let getToken = new Promise((resolve, reject) => {
      wx.getStorage({
        key: 'token',
        success(res) {
          resolve(res.data)
        }
      })
    })

    //生成订单&支付
    getToken.then(
      res => {
        // 获取地址数据
        myReq({
          url: 'http://api_dev.wanxikeji.cn/api/rePayOrder',
          method: "POST",
          data: {
            token: res,
            order_id: JSON.stringify(that.data.goodsInfo.order_id),
          },
        }).then(
          res => {
            console.log("——签名数据源：————>", res);

            let packageinfo = 'prepay_id=' + res.data.data.prepay_id;
            console.log('———预支付id————>', packageinfo);
    
            that.setData({
              timeStamp: res.data.data.timeStamp,
              nonceStr: res.data.data.nonce_str,
              package: packageinfo,
              signType: 'MD5',
              paySign: res.data.data.paySign
            })
            // console.log("—— 时间戳 ————>",that.data.timeStamp);
            // console.log("—— 随机字符 ————>",that.data.nonceStr);
            // console.log("—— 预支付id ————>",that.data.package);
            // console.log("—— 签名 ————>",that.data.paySign);
            //请求支付
            wx.hideLoading()
            wx.requestPayment({
              timeStamp: that.data.timeStamp,
              nonceStr: that.data.nonceStr,
              package: that.data.package,
              signType: that.data.signType,
              paySign: that.data.paySign,
              success: function (res) {
                console.log("支付成功");
                wx.reLaunch({
                  url: '../order_Form/order_Form'
                })
              },
              fail: function (res) {
                console.log("支付失败！");
                // wx.showToast({
                //   title: '支付失败！',
                //   icon: 'none',
                //   duration: 2000
                // })
                wx.reLaunch({
                  url: '../wait_Pay/wait_Pay'
                })
              }
            })    
          },
          err => {
            wx.showToast({
              title: "err",
            })
          }
        )
      }
    )
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    //拿取token及地址列表
    wx.getStorage({
      key: 'token',
      success(res) {
        // console.log(res)
        that.setData({ token: res.data })
      }
    })
    wx.getSystemInfo({
      success: res => {
        //导航高度
        console.log(res)
        let navH = res.statusBarHeight + 182
        let boxH = res.windowHeight - navH
        this.setData({
          navH,
          boxH
        });
      },
      fail(err) {
        wx.showToast({
          title: err,
        })
      }
    })

    //渲染订单地址
    wx.getStorage({
      key: 'goodsInfo',
      success(res) {
        console.log("hksljhjdlk",res.data);
        console.log(JSON.parse(res.data));
        // 请求地址列表 根据地址id拿取地址
    wx.request({
      url: 'http://api_dev.wanxikeji.cn/api/userAddressList',
      data:{
        token:wx.getStorageSync('token')
      },
      success:function (res2) {
        console.log(res2);  
        wx.getStorage({
          key: 'orderDetail',
          success: function(res) {
            let address_id = JSON.parse(res.data).address_id
            console.log(address_id);
        for (let i = 0; i < res2.data.data.length; i++) {
         if (res2.data.data[i].address_id==address_id) {
             that.setData({
              orderAddress:res2.data.data[i].procince+res2.data.data[i].city+res2.data.data[i].area+res2.data.data[i].detailed
             })
             console.log(that.data.address); 
         }    
        }
      }
    })
},
fail(err){
  wx.showToast({
    title: "地址获取失败",
  })
}
})
        that.setData({
          goodsInfo: JSON.parse(res.data),
          // orderUserName: JSON.parse(res.data.childern.name),
          // orderPhone: JSON.parse(res.data.childern.phone),
          // orderAddress: JSON.parse(res.data.childern.detailed),
        })
        let userName = that.data.goodsInfo.name
        let phone = that.data.goodsInfo.phone
        let address = that.data.goodsInfo.detailed
        console.log("——重新支付订单信息User————>", userName);
        that.setData({
          orderUserName: userName,
          orderPhone: phone,
          orderAddress: address,
        })
        console.log("——重新支付订单信息goodsInfo————>", that.data.goodsInfo);

      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */

  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})